package org.huanglei.account.view;

import java.util.Calendar;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TimeZone;

import org.huanglei.account.MyApplication;
import org.huanglei.account.R;
import org.huanglei.account.provider.DataProvider;
import org.huanglei.account.provider.DataProvider.StaticDateFormat;
import org.huanglei.account.provider.DataProviderFactory;
import org.huanglei.account.view.adapter.DashboardAdapter;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 
 * @author huanglei
 * @data Jul 6, 2012
 * @tag 导航界面
 */
public class DashboardActivity extends Activity implements OnItemClickListener {

	private final Context me = this;
	
	private DataProvider dataProvider = null;
	
	private int year,month;
	
	private long mExitTime;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		MyApplication.getInstance().addActivity(this);
		
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.account_dashboard);
		initView();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}

	/**
	 * 初始化控件及相关事件
	 */
	private void initView() {
		
		this.dataProvider = DataProviderFactory.getProvider(me,
				DataProviderFactory.LOCAL);
		
		GridView gridView = (GridView) findViewById(R.id.dashboard_gridview);
		gridView.setAdapter(new DashboardAdapter(me));
		gridView.setOnItemClickListener((OnItemClickListener) me);
		
		done();
	
	}
	
	/**
	 * 加载数据
	 */
	private void done() {
		
		DataProvider dataProvider = DataProviderFactory.getProvider(me,
				DataProviderFactory.LOCAL);
		 
		String month = getTime();
		
		Double sr = 0.0;
		Double zc = 0.0;
		
		Map<String, Double> map = dataProvider.getStatistics(month,StaticDateFormat.MONTH);
		Set<String> key = map.keySet();
		for (Iterator<String> it = key.iterator(); it.hasNext();) {
			String temp = String.valueOf(it.next());
			if("工资".equals(temp)){
				sr += map.get(temp);
			}
			if(!"工资".equals(temp)){
				zc += map.get(temp);
			}
		 }
		 
		//
		TextView total = (TextView) findViewById(R.id.textMonth);
		total.setText(initTime());
		
		//总收入
		TextView tIncome = (TextView) findViewById(R.id.textIncome);
		tIncome.setText("￥"+sr);
		
		//总支出
		TextView zcTextView = (TextView) findViewById(R.id.textExpenditure);
		zcTextView.setText("￥"+zc);	
	}
	
	/**
	 * 获取日期
	 * @return
	 */
	public String getTime(){
		
		Calendar c = Calendar.getInstance(TimeZone.getTimeZone("GMT+08:00"));
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH) + 1;
		if (month < 10) {
			return year + "-0" + month;
		} else {
			return year + "-" + month;
		}
  
	}

	/**
	 * 实现gridview选择事件
	 */
	@Override
	public void onItemClick(AdapterView<?> adapterView, View view,
			int position, long id) {
		String activityName = (String) view.getTag();
		Class<?> clazz = null;
		try {
			if (activityName == null) {
				throw new NullPointerException(activityName);
			}
			clazz = Class.forName(activityName);
		} catch (Exception e) {
			Toast.makeText(getApplication(), "页面没有找到", Toast.LENGTH_SHORT)
					.show();
			Log.e(getString(R.string.app_name), activityName, e);
			return;
		}

		Intent intent = new Intent();
		intent.setClass(DashboardActivity.this, clazz);
		startActivity(intent);
	}
	
	/**
	 * 获取当前年月日
	 */
	private String initTime(){
		
		Calendar c = Calendar. getInstance(TimeZone.getTimeZone("GMT+08:00"));
		year = c.get(Calendar.YEAR);
        month = c.get(Calendar.MONTH)+1;
        return year+"年"+month+"月";
	}
	
	/**
	 * 退出应用
	 */
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		
		switch (keyCode) {
		case KeyEvent.KEYCODE_BACK:
			QuitApp();
			return true;
		}
		return false;
	}
	
	public void QuitApp() {
		new AlertDialog.Builder(DashboardActivity.this).setTitle("提示").setMessage(
				"确定退出个人财务?").setIcon(R.drawable.quit).setPositiveButton("确定",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {	
						
						MyApplication.getInstance().exit();
					}
				}).setNegativeButton("取消",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int whichButton) {
								
							}
						}
				).show();

	}
}
